package com.grace.cms.general.controller.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.grace.cms.common.enums.ArticleTypeEnums;
import com.grace.cms.common.enums.OrderEnums;
import com.grace.cms.common.model.TimeNum;
import com.grace.cms.common.model.support.PageData;
import com.grace.cms.general.taglib.ArticleTag;
import com.grace.cms.query.model.vo.WebArticleVo;
import com.grace.common.core.controller.BaseController;
import com.grace.common.core.domain.AjaxResult;

@CrossOrigin
@RestController
@RequestMapping("/{themeName}/api/article")
public class ArticleApiController extends BaseController {

    @Autowired
    private ArticleTag articleTag;

    /**
     * 获取文章数量（按类型）
     */
    @GetMapping("/selectArticleNumByType/{articleType}")
    public AjaxResult selectArticleNumByType(@PathVariable("themeName") String themeName,
            @PathVariable("articleType") ArticleTypeEnums articleType) {
        return AjaxResult.success(articleTag.selectArticleNumByType(themeName, articleType));
    }

    /**
     * 获取文章总数
     */
    @GetMapping("/selectArticleNum")
    public AjaxResult selectArticleNum(@PathVariable("themeName") String themeName) {
        return AjaxResult.success(articleTag.selectArticleNum(themeName));
    }

    /**
     * 依据年月获取文章数量
     */
    @GetMapping("/listArticleTimeNum/{pageNum}/{pageSize}")
    public AjaxResult listArticleTimeNum(@PathVariable("themeName") String themeName,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        List<TimeNum> result = articleTag.listArticleTimeNum(themeName, pageNum, pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 获取文章Html内容（按ID）
     */
    @GetMapping("/selectWebArticleHtmlById/{articleId}")
    public AjaxResult selectWebArticleHtmlById(@PathVariable("themeName") String themeName,
            @PathVariable("articleId") Long articleId) {
        return AjaxResult.success(articleTag.selectWebArticleHtmlById(articleId, themeName));
    }

    /**
     * 获取文章MarkDown内容（按ID）
     */
    @GetMapping("/selectWebArticleMdById/{articleId}")
    public AjaxResult selectWebArticleMdById(@PathVariable("themeName") String themeName,
            @PathVariable("articleId") Long articleId) {
        return AjaxResult.success(articleTag.selectWebArticleMdById(articleId, themeName));
    }

    /**
     * 获取文章Html内容（按URL）
     */
    @GetMapping("/selectWebArticleHtmlByUrl/{articleUrl}")
    public AjaxResult selectWebArticleHtmlByUrl(@PathVariable("themeName") String themeName,
            @PathVariable("articleUrl") String articleUrl) {
        return AjaxResult.success(articleTag.selectWebArticleHtmlByUrl(articleUrl, themeName));
    }

    /**
     * 获取文章MarkDown内容（按URL）
     */
    @GetMapping("/selectWebArticleMdByUrl/{articleUrl}")
    public AjaxResult selectWebArticleMdByUrl(@PathVariable("themeName") String themeName,
            @PathVariable("articleUrl") String articleUrl) {
        return AjaxResult.success(articleTag.selectWebArticleMdByUrl(articleUrl, themeName));
    }

    /**
     * 按分类ID获取文章列表
     */
    @GetMapping("/listWebArticleByCategoryId/{categoryId}/{pageNum}/{pageSize}")
    public AjaxResult listWebArticleByCategoryId(@PathVariable("themeName") String themeName,
            @PathVariable("categoryId") long categoryId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        PageData<WebArticleVo> result = articleTag.listWebArticleByCategoryId(themeName, categoryId, pageNum, pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 按分类ID获取文章列表（带排序）
     */
    @GetMapping("/listWebArticleByCategoryId/{categoryId}/{pageNum}/{pageSize}/{order}")
    public AjaxResult listWebArticleByCategoryId(@PathVariable("themeName") String themeName,
            @PathVariable("categoryId") long categoryId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable("order") OrderEnums order) {
        PageData<WebArticleVo> result = articleTag.listWebArticleByCategoryId(themeName, categoryId, pageNum, pageSize, order);
        return AjaxResult.success(result);
    }

    /**
     * 按文章类型获取文章列表
     */
    @GetMapping("/listWebArticleByType/{articleType}/{pageNum}/{pageSize}")
    public AjaxResult listWebArticleByType(@PathVariable("themeName") String themeName,
            @PathVariable("articleType") Integer articleType,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        PageData<WebArticleVo> result = articleTag.listWebArticleByType(themeName, articleType, pageNum, pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 按文章类型获取文章列表（带排序）
     */
    @GetMapping("/listWebArticleByType/{articleType}/{pageNum}/{pageSize}/{order}")
    public AjaxResult listWebArticleByType(@PathVariable("themeName") String themeName,
            @PathVariable("articleType") Integer articleType,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable("order") OrderEnums order) {
        PageData<WebArticleVo> result = articleTag.listWebArticleByType(themeName, articleType, pageNum, pageSize, order);
        return AjaxResult.success(result);
    }

    /**
     * 获取文章列表
     */
    @GetMapping("/pageArticles/{pageNum}/{pageSize}")
    public AjaxResult pageArticles(@PathVariable("themeName") String themeName,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        PageData<WebArticleVo> result = articleTag.pageArticles(themeName, pageNum, pageSize);
        return AjaxResult.success(result);
    }

    /**
     * 获取文章列表（带排序）
     */
    @GetMapping("/pageArticles/{pageNum}/{pageSize}/{order}")
    public AjaxResult pageArticles(@PathVariable("themeName") String themeName,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable("order") OrderEnums order) {
        PageData<WebArticleVo> result = articleTag.pageArticles(themeName, pageNum, pageSize, order);
        return AjaxResult.success(result);
    }

    /**
     * 按时间分组获取文章列表
     */
    @GetMapping("/groupOrderByTime/{crTime}/{pageNum}/{pageSize}")
    public AjaxResult groupOrderByTime(@PathVariable("themeName") String themeName,
            @PathVariable("crTime") String crTime,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize) {
        PageData<WebArticleVo> result = articleTag.groupOrderByTime(themeName, crTime, pageNum, pageSize);
        return AjaxResult.success(result);
    }
}
